package cn.xz.zgsg.activity

import android.os.Handler
import android.widget.SeekBar
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import cn.xz.zgsg.R
import cn.xz.zgsg.adapter.DeviceTAdapter
import cn.xz.zgsg.adapter.LocalListAdapter
import cn.xz.zgsg.adapter.SJSAdapter
import cn.xz.zgsg.base.BaseActivity
import cn.xz.zgsg.base.BaseApplication
import cn.xz.zgsg.bean.*
import cn.xz.zgsg.databinding.ActivityMain2Binding
import cn.xz.zgsg.net.ControlDevice
import cn.xz.zgsg.net.OkHttpUtil
import cn.xz.zgsg.util.*

/**
 * 二楼界面*/
class Main2Activity: BaseActivity<ActivityMain2Binding>(R.layout.activity_main2){

    //标识符
    private var mTag = 0
    //设备信息
    private var mDevice: DeviceList? = null
    //当前视频进度值
    private var pMove = 0
    private var handler: Handler? = null

    private var mAdapter: LocalListAdapter? = null

    override fun initData() {
        //获取跳转数据
        mTag = intent.getIntExtra(CommonLib.JUMP_TYPE, -1)
        //获取当前设备信息
        if (mTag != -1){
            mDevice = ControlDevice.getThisDevice(mTag)
            //mDevice!!.ip = "192.168.140.248"
        }
        //如果数据异常直接退出
        if (mTag == -1 || mDevice == null){
            ToastUtil.show("当前数据异常")
            finish()
        }else{
            //显示对应视图
            showView()
            //如果是多级列表
            //设置层级
            setList()
            getLocalList()

            //开始获取声音和进度
            //获取当前音量
            getVoice()
            //开启定时器获取视频进度
            handler = Handler()
            handler?.post(movieR)
        }

    }

    override fun initEvent() {
        //返回
        mDataBind.tvBack.setOnClickListener {
            finish()
        }
        //一键打开
        mDataBind.tvQyjdk.setOnClickListener {
            if (mTag == 14062){
                ToastUtil.show("程序循环")
            }else{
                LocalHttp.playStatus(mDevice!!.ip,3)
                LocalHttp.playStatus(mDevice!!.ip,5)
                setPlay(10)
            }

        }
        //播放
        mDataBind.zkPlay.setOnClickListener {
            setPlay(1)
        }
        //暂停
        mDataBind.zkPause.setOnClickListener {
            setPlay(2)
        }
        //重播
        mDataBind.zkReplay.setOnClickListener {
            setPlay(3)
        }
        //上一个
        mDataBind.tvQsyy.setOnClickListener {
            LocalHttp.getTMData(this,mDevice!!.ip,object :LocalHttp.GetTMListener{
                override fun getData(tMList: TMBean.TMList?) {
                    if (tMList?.index != 0){
                        setPlay(9)
                        mAdapter!!.setPosition(tMList!!.index - 1)
                    }
                }
            })
        }
        //下一个
        mDataBind.tvQxyy.setOnClickListener {
            LocalHttp.getTMData(this,mDevice!!.ip,object :LocalHttp.GetTMListener{
                override fun getData(tMList: TMBean.TMList?) {
                    if (tMList?.index != mAdapter!!.data!!.size-1){
                        setPlay(10)
                        mAdapter!!.setPosition(tMList!!.index + 1)
                    }
                }
            })
        }
        //声音滑动监听
        mDataBind.zkVolumeSeekbar.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                SeekBarPopUtils.move(progress, seekBar, false)
                LocalHttp.changeVoice(mDevice!!.ip,progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.showPop(seekBar)
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.dismiss()
            }
        })
        //视频滑动监听
        mDataBind.zkVideoSeekbar.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                SeekBarPopUtils.move(progress, seekBar, true)
                pMove = progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.showPop(seekBar)
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.dismiss()
                LocalHttp.changeMovie(mDevice!!.ip,pMove)
            }
        })
        //播放
        mDataBind.zkPlay88.setOnClickListener {
            U3dHttp.PlayMovie("192.168.10.29")
        }
        //暂停
        mDataBind.zkPause88.setOnClickListener {
            U3dHttp.PauseMovie("192.168.10.29")
        }
        //重播
        mDataBind.zkReplay88.setOnClickListener {
            U3dHttp.ReplayMovie("192.168.10.29")
        }
        //鼠键
        mDataBind.zkSj.setOnClickListener {
            ARouter.to(SJActivity::class.java,"192.168.10.29")
        }
        //声音滑动监听
        mDataBind.zkVolumeSeekbar88.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                SeekBarPopUtils.move(progress, seekBar, false)
                LocalHttp.changeVoice("192.168.10.29",progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.showPop(seekBar)
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.dismiss()
            }
        })
        //视频滑动监听
        mDataBind.zkVideoSeekbar88.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                SeekBarPopUtils.move(progress, seekBar, true)
                pMove = progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.showPop(seekBar)
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.dismiss()
                U3dHttp.changeMovie("192.168.10.29",pMove)
            }
        })

    }


    private fun setList() {
        //这个集合存储的是每一层级的样式以及信息，会根据该集合的长度去创建最大多少级
        val mList: ArrayList<GetListUtils.GetListData> = ArrayList<GetListUtils.GetListData>()
        mList.add(GetListUtils.GetListData(itemSize = 16f, leftMargin = 20))
        GetListUtils.find(mDataBind.clAll, this, mList)

        //设置一级目录
        val mSXList: ArrayList<GetListUtils.TestData> = ArrayList<GetListUtils.TestData>()
        mSXList.add(GetListUtils.TestData("数联网",14059))
        mSXList.add(GetListUtils.TestData("数据发票",14060))
        mSXList.add(GetListUtils.TestData("区块链",14061))
        mSXList.add(GetListUtils.TestData("数据交易平台",14062))
        mSXList.add(GetListUtils.TestData("算力",14063))
        mSXList.add(GetListUtils.TestData("应用场景1",14064))
        mSXList.add(GetListUtils.TestData("应用场景2",14065))
        mSXList.add(GetListUtils.TestData("应用场景3",14066))
        mSXList.add(GetListUtils.TestData("应用场景4",14067))

        //设置默认哪个选中
        mSXList.forEach {
            if (it.Tag == mDevice!!.id){
                it.select = true
            }
        }
        GetListUtils.setListData(1, mSXList, object : GetListUtils.OnListener {
            override fun onClick(l: GetListUtils.TestData) {
                mTag  = l.Tag!!
                mDevice = ControlDevice.getThisDevice(mTag)
                //mDevice!!.ip = "192.168.140.248"
                //mDevice!!.ip = "192.168.0.107"
                //mDevice!!.ip = "192.168.10.23"
                //获取本地列表
                getLocalList()
                //显示对应视图
                showView()
            }
        })
    }

    private fun showView(){
        mDataBind.clOther.gone()
        mDataBind.clSjs.gone()
        setOn()
        //默认显示哪一个
        when(mTag){
            14062->{
                //数数据交易平台,
                mDataBind.clSjs.visible()
            }
            else->{
                mDataBind.clOther.visible()
            }
        }
    }

    private fun setPlay(i :Int){
        LocalHttp.moviePlay(mDevice!!.ip,i)
    }

    private fun setPlay(i :Int,p: Int){
        LocalHttp.moviePlay1(mDevice!!.ip,p,i)
    }

    //获取本地资源列表
    var sjAdapter: SJSAdapter ?= null
    private fun getLocalList(){
        if (mTag == 14062){
            //交易所
            mDataBind.rvOtherList88.layoutManager = GridLayoutManager(this,4)
            mDataBind.rvOtherList88.itemAnimator = DefaultItemAnimator()
            sjAdapter = SJSAdapter()
            mDataBind.rvOtherList88.adapter = sjAdapter
            sjAdapter?.setOnItemClickListener { adapter, view, position ->
                //OkHttpUtil.post("http://192.168.140.248:8030/?act=unity&states=SelectTopic&object=$position")
                OkHttpUtil.post("http://192.168.10.29:8030/?act=unity&states=SelectTopic&object=$position")
                OkHttpUtil.post("http://192.168.10.36:8030/?act=unity&states=SelectTopic&object=$position")
            }

            //获取程序列表数据
            U3dHttp.getU3dData(this,"192.168.10.36",object : U3dHttp.GetU3dDataListener{
                override fun getData(mBean: U3dListBean?) {
                    if (mBean != null){
                        mDataBind.rvOtherList88.post {
                            setMList(mBean)
                        }
                    }
                }
            })

        }else{
            LocalHttp.getPlayData(this,mDevice!!.ip, LocalHttp.GetPlayDataListener {
                if (it.isNotEmpty()){
                    mDataBind.rvOtherList.post {
                        //成功进展，合规沙盒
                        mDataBind.rvOtherList.layoutManager = LinearLayoutManager(BaseApplication.getContext())
                        mDataBind.rvOtherList.itemAnimator = DefaultItemAnimator()
                        mAdapter = LocalListAdapter(mDevice!!)
                        mDataBind.rvOtherList.adapter = mAdapter
                        mAdapter!!.setList(it)
                    }
                }
            })
        }

    }


    private fun getMove(){
        LocalHttp.getMovieSize(mDevice!!.ip,LocalHttp.GetMovieSizeListener { maxTxt, minTxt, maxSeekBar, seekProgress ->
            mDataBind.zkVideoSeekbar.post {
                mDataBind.zkMaxTime.text = maxTxt
                mDataBind.zkMinTime.text = minTxt
                mDataBind.zkVideoSeekbar.max = maxSeekBar
                mDataBind.zkVideoSeekbar.progress = seekProgress
            }
        })
        U3dHttp.getMovieSize(mDevice!!.ip,U3dHttp.GetMovieSizeListener { maxTxt, minTxt, maxSeekBar, seekProgress ->
            mDataBind.zkVideoSeekbar.post {
                mDataBind.zkMaxTime88.text = maxTxt
                mDataBind.zkMinTime88.text = minTxt
                mDataBind.zkVideoSeekbar88.max = maxSeekBar
                mDataBind.zkVideoSeekbar88.progress = seekProgress
            }
        })
    }

    private var movieR: Runnable = object : Runnable {
        override fun run() {
            getMove()
            handler?.postDelayed(this, 1000)
        }
    }

    private fun getVoice(){
        LocalHttp.getVoiceSize(mDevice!!.ip,LocalHttp.GetVoiceSizeListener {
            mDataBind.zkVolumeSeekbar.post {
                mDataBind.zkVolumeSeekbar.progress = it
                mDataBind.zkVolumeSeekbar88.progress = it
            }
        })
    }


    private fun setOn(){
        if (handler != null){
            handler?.removeCallbacks(movieR)
            handler = null
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        setOn()
    }



    private fun setMList(response: U3dListBean) {
        var mPTag = 0
        val bhList: List<U3dListBean.BhList> = response.list
        val subColumns: List<U3dListBean.SubColumns> = response.subColumns
        //筛选出父类
        for (b in subColumns) {
            if (0 == b.parentTag) {
                mPTag = b.tag
            }
        }
        //筛选出对应的二级数据
        val mSXList: ArrayList<GetListUtils.TestData> = ArrayList<GetListUtils.TestData>()
        for (b in subColumns) {
            if (mPTag == b.parentTag) {
                mSXList.add(GetListUtils.TestData(b.name, b.tag, b.parentTag))
            }
        }
        if (mSXList.isEmpty()) { //无数据，从list里面筛选
            for (h in bhList) {
                if (h.tag == mPTag) {
                    mSXList.add( GetListUtils.TestData(h.title, h.id, h.tag))
                }
            }
        }
        sjAdapter?.setList(mSXList)
    }
}